﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;



namespace MedialLibrary
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public static DependencyProperty some_int;

        ContextMenu menu;

        static Window1()
        {
            some_int = DependencyProperty.Register("ExpInt", typeof(int), typeof(Window1), new FrameworkPropertyMetadata(10, FrameworkPropertyMetadataOptions.AffectsRender));
            
        }
        public Window1()
        {
           InitializeComponent();
           menu = new ContextMenu();
           menu.Items.Add("asdf");
        }
        public int ExpInt
        {
            get { return (int)GetValue(some_int); }
            set { SetValue(some_int, value); }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            albumControl1.ViewType = AlbumElement.AlbumControl.TypeOfView.Album;
        }

        private void BtnMinimize_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        Double MouseX, MouseY;

       

        private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MouseX = (double)GetValue(LeftProperty) + e.GetPosition(this).X;
            MouseY = (double)GetValue(TopProperty) + e.GetPosition(this).Y;
        }

        private void Rectangle_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton== MouseButtonState.Pressed)
            {
                Point p = new Point();
                double val = (double)GetValue(LeftProperty);
                p.X = (double)GetValue(LeftProperty) + e.GetPosition(this).X;
                p.Y = (double)GetValue(TopProperty) + e.GetPosition(this).Y;
                SetValue(LeftProperty, (double)GetValue(LeftProperty) + p.X - MouseX);
                SetValue(TopProperty, (double)GetValue(TopProperty) + p.Y - MouseY);
                MouseX = p.X;
                MouseY = p.Y;
            } 
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            albumControl1.ViewType = AlbumElement.AlbumControl.TypeOfView.Add;
        }

        private void Rectangle_MouseMove_1(object sender, MouseEventArgs e)
        {
            if (RectRight.IsMouseCaptured)
            {
                double val = e.GetPosition(this).X - MouseX;
                Width += val;
                MouseX = e.GetPosition(this).X;
            }
        }
        
        private void Rectangle_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
           

                MouseX = e.GetPosition(this).X;
                RectRight.CaptureMouse();
            
            
        }

        

        private void RectRight_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Released)
            {
                Mouse.Capture(null);
                
                
            }
        }

        private void RectDown_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MouseY = e.GetPosition(this).Y;
            RectDown.CaptureMouse();
        }

        private void RectDown_MouseMove(object sender, MouseEventArgs e)
        {
            if (RectDown.IsMouseCaptured)
            {
                double val = e.GetPosition(this).Y - MouseY;
                Height += val;
                MouseY = e.GetPosition(this).Y;
            }
        }

        private void rectangle1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MouseX = e.GetPosition(this).X;
            RectLeft.CaptureMouse();
        }

        private void RectLeft_MouseMove(object sender, MouseEventArgs e)
        {
            if (RectLeft.IsMouseCaptured)
            {
                
                double val = e.GetPosition(this).X + MouseX;
                //SetValue(LeftProperty, (double)GetValue(LeftProperty) + val);
                Width -= val;
                
                MouseX = e.GetPosition(this).X;

            }
        }

        private void RectLeft_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Mouse.Capture(null);
        }
        int col;
        
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            col = (int)(MainField.Width / MainField.ItemWidth);
            MainField.ItemWidth += (MainField.Width - MainField.ItemWidth * col) / col;
            MainField.Height = MainField.ItemHeight * MainField.Children.Count / col;
        }

        private void MainField_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if ((e.NewSize.Width - e.PreviousSize.Width) < 0 && MainField.ItemWidth <= albumControl1.Width)
                return;
            int new_col = (int)(MainField.Width / albumControl1.Width);
            if (new_col != col)
            {
                col = new_col;
                MainField.ItemWidth = albumControl1.Width;
            }
           
            MainField.ItemWidth += (MainField.Width - MainField.ItemWidth * col) / col;
        }

        private void albumControl1_AddButtonClick(object sender, RoutedEventArgs e)
        {

            albumControl1.btnAdd.ContextMenu.IsOpen = true;
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (e.PreviousSize.Width != 0)
            {
                if (e.WidthChanged)
                {
                    double val = e.NewSize.Width - e.PreviousSize.Width;

                    RectMain.Width += val;
                    MainField.Width += val;
                    SideBar.SetValue(LeftProperty, (double)SideBar.GetValue(LeftProperty) + val);
                    RectRight.SetValue(LeftProperty, (double)RectRight.GetValue(LeftProperty) + val);
                    btnClose.SetValue(LeftProperty, (double)btnClose.GetValue(LeftProperty) + val);
                    BtnMinimize.SetValue(LeftProperty, (double)BtnMinimize.GetValue(LeftProperty) + val);
                    MainCnvs.Width += val;
                }

                if (e.HeightChanged)
                {
                    double val = e.NewSize.Height - e.PreviousSize.Height;
                    RectMain.Height += val;
                    MainField.Height += val;
                    SideBar.Height += val;
                    RectRight.Height += val;
                    MainCnvs.Height += val;
                    RectDown.SetValue(TopProperty, (double)RectDown.GetValue(TopProperty) + val);
                    ScrlMain.Height += val;
                }
            }

            
        }

        
    }
}
